package gurps;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import gurps.Character;
import guestbook.PMF;

public class AddCharacterServlet extends HttpServlet 
{
    private static final Logger log = Logger.getLogger(AddCharacterServlet.class.getName())

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        String name = req.getParameter("name");
        String description = req.getParameter("description");
        int st = Integer.parseInt( req.getParameter("strength") );
        int dx = Integer.parseInt( req.getParameter("dexterity") );
        int iq = Integer.parseInt( req.getParameter("intelligence") );
        int ht = Integer.parseInt( req.getParameter("health") );
        Date date = new Date();
		Character c = new Character(user, name, description, date, st, dx, iq, ht);
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
        try 
		{
	    	pm.makePersistent(c);
        } 
		finally 
		{
            pm.close();
        }

        resp.sendRedirect("/characters.jsp");
    }
}